package com.jnu.student.myfirstapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.jnu.student.myfirstapplication.data.GameEngine;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GameViewFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GameViewFragment extends Fragment implements SurfaceHolder.Callback, View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private GameEngine gameEngine;
    private TextView scoreTextView;
    private int score;
    public GameViewFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GameFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GameViewFragment newInstance(String param1, String param2) {
        GameViewFragment fragment = new GameViewFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.fragment_game_view, container, false);
        surfaceView = rootView.findViewById(R.id.surfaceview_game);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);

        scoreTextView = rootView.findViewById(R.id.scoreTextView);
        scoreTextView.setText("书本数：0");
        score = 0;

        Button resetButton = rootView.findViewById(R.id.button_game_reset);
        resetButton.setOnClickListener(this);
        // Inflate the layout for this fragment
        return rootView;
    }
    public void surfaceCreated(SurfaceHolder holder) {
        gameEngine = new GameEngine(surfaceHolder, new GameEngine.GameEventListener() {
            @Override
            public void onHit() {
                score++;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        scoreTextView.setText("书本数：" + score);
                    }
                });
            }
        });
        gameEngine.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // 处理SurfaceView尺寸变化的逻辑
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        gameEngine.stop();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_game_reset) {
            score = 0;
            scoreTextView.setText("书本数：0");
        }
    }
}
